import {createStore} from 'redux'

const reducer=(prevState={
    isShow:false
},action={})=>{
    let newState={...prevState}
    switch(action.type){
        case 'test-show':
            newState.isShow=true
            return newState;
        case 'test-hidden':
            newState.isShow=false
            return newState;
        default:return prevState
    }

}
const store=createStore(reducer)

export default store


function sanhuamaoCreateStore(reducer){
    let state=reducer() //初始状态、旧状态

    //订阅
    let callbackList=[]
    function subscribe(callback){
        callbackList.push(callback)
    }

    //分发
    function dispatch(action){
        state=reducer(state,action) //处理事件并覆盖状态
        //通知订阅者
        for(let i in callbackList){
            callbackList[i]&&callbackList[i]()
        }
    }

    //返回状态
    function getState(){
        return state
    }

    return{
        subscribe,
        dispatch,
        getState
    }


}